package com.changgou.goods.controller;

import com.changgou.core.controller.AbstractCoreController;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品表（华为P40 蓝色 256G） 具体的某个商品
 * @author liyipeng
 * @date 2021-06-26 17:00
 */
@RestController
@RequestMapping("/sku")
@CrossOrigin
public class SkuController extends AbstractCoreController<Sku>{

    private SkuService  skuService;

    @Autowired
    public SkuController(SkuService  skuService) {
        super(skuService, Sku.class);
        this.skuService = skuService;
    }


    /***
     * 根据审核状态查询Sku
     */
    @GetMapping("/status/{status}")
    public Result<List<Sku>> findByStatus(@PathVariable(name = "status") String status){
        List<Sku> list = skuService.findByStatus(status);
        return new Result<List<Sku>>(true, StatusCode.OK,"查询成功",list);
    }

    /**
     *  给指定的商品的ID 扣库存
     * @param id  要扣库存的商品的ID skuid
     * @param num  要扣的数量
     * @return
     */
    @GetMapping("/decCount")
    public Result decCount(@RequestParam(name="id") Long id, @RequestParam(name="num") Integer num){
        int count =  skuService.decCount(id,num);
        if (count > 0) {
            return new Result<List<Sku>>(true, StatusCode.OK,"扣库存成功") ;
        }
        return new Result<List<Sku>>(false, StatusCode.ERROR,"扣库存失败") ;
    }

}
